/*
 * @Author: luojianfeng
 * @Date: 2025-01-14 16:24:39
 * @LastEditTime: 2025-01-14 16:39:03
 * @LastEditors: luojianfeng
 * @Description:
 */
/**
 * @description: convertNum 数字单位格式化
 * @param {number} num
 * @param {*} needUnit
 * @param {*} locale
 * @return {*}
 */
export function convertNum(num: number, needUnit = true, locale = 'en-US') {
  if (needUnit) {
    if (locale === 'en-US') {
      if (Math.abs(num) / 1000 / 1000 / 1000 > 1) {
        return (num / 1000 / 1000 / 1000).toFixed(2) + ' billion'
      } else if (Math.abs(num) / 1000 / 1000 > 1) {
        return (num / 1000 / 1000).toFixed(2) + ' million'
      } else if (Math.abs(num) / 1000 > 1) {
        return Math.round(num / 1000) + ' thousand'
      } else {
        return Math.round(num) + ''
      }
    } else {
      if (Math.abs(num) / 10000 / 10000 > 1) {
        return (num / 10000 / 10000).toFixed(2) + ' 亿'
      } else if (Math.abs(num) / 10000 > 1) {
        return (num / 10000).toFixed(2) + ' 万'
      } else {
        return num + ''
      }
    }
  } else {
    if (locale === 'en-US') {
      if (Math.abs(num) / 1000 / 1000 / 1000 > 1) {
        return (num / 1000 / 1000 / 1000).toFixed(2)
      } else if (Math.abs(num) / 1000 / 1000 > 1) {
        return (num / 1000 / 1000).toFixed(2)
      } else if (Math.abs(num) / 1000 > 1) {
        return Math.round(num / 1000)
      } else {
        return Math.round(num)
      }
    } else {
      if (Math.abs(num) / 10000 / 10000 > 1) {
        return (num / 10000 / 10000).toFixed(2)
      } else if (Math.abs(num) / 10000 > 1) {
        return (num / 10000).toFixed(2)
      } else {
        return num
      }
    }
  }
}
/**
 * @description: convertNumUnit 获取单位
 * @param {number} n
 * @param {*} locale
 * @return {*}
 */
export function convertNumUnit(n: number, locale = 'en-US') {
  const num = Math.abs(n)
  if (locale === 'en-US') {
    if (num / 1000 / 1000 / 1000 > 1) {
      return 'billion'
    } else if (num / 1000 / 1000 > 1) {
      return 'million'
    } else if (num / 1000 > 1) {
      return 'thousand'
    } else {
      return ''
    }
  } else {
    if (num / 10000 / 10000 > 1) {
      return '亿'
    } else if (num / 10000 > 1) {
      return '万'
    } else {
      return ''
    }
  }
}
/**
 * @description: convertStorageUnit 计算机存储单位转换
 * @param {number} num
 * @return {*}
 */
export function convertStorageUnit(num: number) {
  if (num / 1024 / 1024 / 1024 / 1024 > 1) {
    return 'TB'
  } else if (num / 1024 / 1024 / 1024 > 1) {
    return 'GB'
  } else if (num / 1024 / 1024 > 1) {
    return 'MB'
  } else if (num / 1024 > 1) {
    return 'KB'
  } else {
    return 'B'
  }
}
/**
 * @description: convertStorageNum 根据计算机单位转换数字
 * @param {string} unit
 * @param {number} num
 * @return {*}
 */
export function convertStorageNum(unit: string, num: number) {
  switch (unit) {
    case 'KB':
      return Math.round(num / 1024)
    case 'MB':
      return (num / 1024 / 1024).toFixed(2)
    case 'GB':
      return (num / 1024 / 1024 / 1024).toFixed(2)
    case 'TB':
      return (num / 1024 / 1024 / 1024 / 1024).toFixed(2)
    default:
      return num
  }
}
// 文件大小单位转换
export function convertStorageSize(num: number) {
  if (num / 1024 / 1024 / 1024 / 1024 > 1) {
    return (num / 1024 / 1024 / 1024 / 1024).toFixed(2) + ' TB'
  } else if (num / 1024 / 1024 / 1024 > 1) {
    return (num / 1024 / 1024 / 1024).toFixed(2) + ' GB'
  } else if (num / 1024 / 1024 > 1) {
    return (num / 1024 / 1024).toFixed(2) + ' MB'
  } else if (num / 1024 > 1) {
    return Math.round(num / 1024) + ' KB'
  } else {
    return `${Number(num)} B`
  }
}
